﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper_Core.BLL.Impl;
using Dapper_Core.Common;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Dapper_Core.Web.Areas.Sys.Controllers
{
    public class Sys_RoleController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult GetPageList(int rowsPerPage, int offsetRows, string search, string sortName, string sortOrder)
        {
            try
            {
                if (rowsPerPage < 1 || offsetRows < 0)
                {
                    return Json(new { total = 0, rows = new List<Sys_Role>() });
                }
                (List<Sys_Role> roles, int count) = new Sys_RoleBusiness().GetPageList(offsetRows / rowsPerPage, rowsPerPage, search, sortName + " " + sortOrder);
                return Json(new { total = count, rows = roles });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取角色分页信息失败", $"rowsPerPage={rowsPerPage}，offsetRows={offsetRows}，search={search}，sortName={sortName}，sortOrder={sortOrder}", ex, "DapperCore");
                return Json(new { total = 0, rows = new List<Sys_Role>() });
            }
        }

        public IActionResult GetRolePermissionList(string userCode)
        {
            try
            {
                List<Sys_ZTreeMenu> menus = new Sys_RoleBusiness().GetRolePermissionList(userCode);
                return Json(menus);
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取“角色 - 用户有权访问的角色”之间的对应关系失败", "userCode=" + userCode, ex, "DapperCore");
                return Json(new List<Sys_ZTreeMenu>());
            }
        }

        [HttpPost]
        public IActionResult Add(Sys_Role role)
        {
            try
            {
                Sys_RoleBusiness bussiness = new Sys_RoleBusiness();
                if (bussiness.IsExist(role.RoleCode))
                {
                    return Json(new { Success = false, Message = "本角色已存在，请修改角色编码！" });
                }
                #region 完善Sys_Role
                role.OrderNo = role.RoleCode;
                role.IsActive = 1;
                role.IsDeleted = 0;
                role.Creator = SessionUtility.GetBasicUserInfo().UserName;
                role.CreateDate = DateTime.Now;
                #endregion
                bool success = bussiness.Add(role);
                if (success)
                {
                    return Json(new { Success = true, Message = "添加成功！" });
                }
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统角色添加失败", "role=" + JsonConvert.SerializeObject(role), ex, "DapperCore");
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
        }

        [HttpPost]
        public IActionResult Edit(Sys_Role role)
        {
            try
            {
                Sys_RoleBusiness bussiness = new Sys_RoleBusiness();
                #region 完善Sys_Role
                Sys_Role old = bussiness.Get(role.RoleCode);
                old.RoleName = role.RoleName;
                old.RoleType = role.RoleType;
                role.OrderNo = role.RoleCode;
                old.Remarks = role.Remarks;
                old.Modifier = SessionUtility.GetBasicUserInfo().UserName;
                old.ModifyDate = DateTime.Now;
                #endregion
                bool success = bussiness.UpdateNotNull(old);
                if (success)
                {
                    return Json(new { Success = true, Message = "修改成功！" });
                }
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统角色修改失败", "role=" + JsonConvert.SerializeObject(role), ex, "DapperCore");
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
        }

        [HttpPost]
        public IActionResult Delete(string ids)
        {
            try
            {
                bool success = new Sys_RoleBusiness().Delete(ids, SessionUtility.GetBasicUserInfo().UserName);
                if (success)
                {
                    return Json(new { Success = true, Message = "删除成功！" });
                }
                return Json(new { Success = false, Message = "删除失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统角色删除失败", "ids=" + ids, ex, "DapperCore");
                return Json(new { Success = false, Message = "删除失败，请联系管理员！" });
            }
        }
    }
}